﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace TimViec.NTD
{
    public partial class TimHocSo : System.Web.UI.Page
    {
        public int nganhngheid { get; set; }
        public int noiLVid { get; set; }
        public string tuKhoa { get; set; }
        public int trang { get; set; }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["NTD"] == null)
            {
                Response.Redirect("/nha-tuyen-dung.html");
            }
            if (Request.QueryString["cate"] != null)
            {
                nganhngheid = int.Parse(Request.QueryString["cate"]);
            }
            else
            {
                nganhngheid = -1;
            }
            if (Request.QueryString["nlv"] != null)
            {
                noiLVid = int.Parse(Request.QueryString["nlv"]);
            }
            else
            {
                noiLVid = -1;
            }
            if (Request.QueryString["page"] != null)
            {
                trang = int.Parse(Request.QueryString["page"]);
            }
            else
            {
                trang = 1;
            }
            if (Request.QueryString["tukhoa"] != null)
            {
                tuKhoa = Request.QueryString["tukhoa"];
            }
            else
            {
                tuKhoa = "";
            }
            if (!IsPostBack)
            {
                LoadData();
                LoadDataSearch(nganhngheid, noiLVid, tuKhoa);
            }
        }
        public void LoadData()
        {
            ListItem itemNganhNge = new ListItem();
            itemNganhNge.Text = "----Tất cả ngành nghề----";
            itemNganhNge.Value = "-1";
            itemNganhNge.Selected = true;
            ListItem itemNoiLV = new ListItem();
            itemNoiLV.Text = "----Tất cả nơi làm việc----";
            itemNoiLV.Value = "-1";
            itemNoiLV.Selected = true;
            //
            droNganhNghe.DataSource = BLL.Admin.NganhNgheHelper.List_NganhNghe_ValueConfig(0);
            droNganhNghe.DataTextField = "Ten";
            droNganhNghe.DataValueField = "NganhNgheId";
            droNganhNghe.DataBind();
            droNganhNghe.Items.Add(itemNganhNge);
            //
            droNoiLamViec.DataSource = BLL.Admin.TinhHelper.List_ALL_Tinh();
            droNoiLamViec.DataTextField = "Ten";
            droNoiLamViec.DataValueField = "TinhId";
            droNoiLamViec.DataBind();
            droNoiLamViec.Items.Add(itemNoiLV);
        }
        public void LoadDataSearch(int nganhNgheid, int noilv, string tukhoatk)
        {
            ltlPaging.Text = "";
            string path = HttpContext.Current.Request.Url.AbsolutePath;
            DAL.UserTuyenDung user = BLL.TuyenDung.UserHelper.Get_UserEmail(Session["NTD"].ToString());
            int page = 1;
            droNganhNghe.SelectedValue = nganhNgheid.ToString();
            droNoiLamViec.SelectedValue = noiLVid.ToString();
            txtSearch.Text = tuKhoa;
            if (Request.QueryString["page"] != null)
            {
                page = int.Parse(Request.QueryString["page"]);
            }
            rptList.DataSource = BLL.TuyenDung.ViewHelper.List_HoSoTimKiem(tukhoatk, nganhNgheid, noilv, page, 10);

            rptList.DataBind();
            int Count = BLL.TuyenDung.ViewHelper.CountList_HoSoTimKiem(tukhoatk, nganhNgheid, noilv);
            int TotalPage = Count / 10 + 1;
            if (Count < 1)
            {
                lbtPreview.Visible = false;
                lbtNext.Visible = false;
            }
            if (page == 1)
            {
                lbtPreview.Visible = false;
            }
            if (page == TotalPage)
            {
                lbtNext.Visible = false;
            }
            for (int i = page - 4; i < page + 4; i++)
            {
                if (i < 1)
                    continue;
                if (i > TotalPage)
                    continue;
                if (i == page && Count > 0)
                    ltlPaging.Text += "<li><a class='current' href='" + path + "?cate=" + nganhngheid + "&nlv=" + noiLVid + "&tukhoa=" + tuKhoa + "&page=" + i + "'>" + i + "</a></li>";
                else
                    ltlPaging.Text += "<li><a href='" + path + "?cate=" + nganhngheid + "&nlv=" + noiLVid + "&tukhoa=" + tuKhoa + "&page=" + i + "'>" + i + "</a></li>";
            }
        }

        protected void lbtPreview_Click(object sender, EventArgs e)
        {
            string path = HttpContext.Current.Request.Url.AbsolutePath;
            Response.Redirect(path + "?cate=" + nganhngheid + "&nlv=" + noiLVid + "&tukhoa=" + tuKhoa + "&page=" + (trang - 1));
        }

        protected void lbtNext_Click(object sender, EventArgs e)
        {
            string path = HttpContext.Current.Request.Url.AbsolutePath;
            Response.Redirect(path + "?cate=" + nganhngheid + "&nlv=" + noiLVid + "&tukhoa=" + tuKhoa + "&page=" + (trang + 1));
        }

        protected void btSearch_Click(object sender, EventArgs e)
        {
            LoadDataSearch(int.Parse(droNganhNghe.SelectedValue), int.Parse(droNoiLamViec.SelectedValue), txtSearch.Text);
        }

        protected void btSaveHoSo_Click(object sender, EventArgs e)
        {
            DAL.UserTuyenDung user = BLL.TuyenDung.UserHelper.Get_UserEmail(Session["NTD"].ToString());
            foreach (RepeaterItem item in rptList.Items)
            {
                HiddenField hiHoSoId = item.FindControl("hiHoSoId") as HiddenField;
                CheckBox checkList = item.FindControl("checkList") as CheckBox;
                if (checkList.Checked == true)
                {
                    BLL.TuyenDung.ViewHelper.LuHoSoTimKiem(int.Parse(hiHoSoId.Value), user.UserId);
                }
            }
            ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe", "alert('Lưu hồ sơ thành công!');", true);

        }

    }
}